from django.db import models
from django.contrib.auth.models import User
from tagging.fields import TagField
from tagging.models import Tag

class Site(models.Model):
	site_id = models.IntegerField(unique=True)
	name = models.CharField(max_length=50)
	assigned_techs = models.ManyToManyField(User, null=True)
	
	def __unicode__(self):
		return "[%s] %s" % (self.id, self.name)
		
	def guess_site(request):
		client_address = request.get_host()
		client_address = client_address.split('.')
		try:
			s = Site.objects.get(site_id=client_address[1])
		except (KeyError, Site.DoesNotExist):
			s = Site.objects.get(site_id=0)			
		return s
		
	guess_site = staticmethod(guess_site)
	
class CheckIn(models.Model):
	tech = models.ForeignKey(User, editable=False)
	site = models.ForeignKey(Site)
	#TODO: Strip time, keep date only?
	dt = models.DateTimeField('Check In Date/Time',False,True,editable=False)
	
	def __unicode__(self):
		return "%s %s at %s at %s" % (self.tech.first_name, self.tech.last_name, self.site.name, self.dt)

# Notes seem to be obsolete with the discovery of django's built-in comments app.
#		
#class Note(models.Model):
#	created_dt = models.DateTimeField('Note Created',False,True,editable=False)
#	tech = models.ForeignKey(User,editable=False)
#	description = models.TextField()
#
#	def __unicode__(self):
#		return "[%s] Created %s by %s: %s" % (self.id, self.created_dt, self.tech.name, self.description)

class Device(models.Model):
	#### Notes
	# Each of the _tech fields have backwards relations disabled
	# because we really don't need to look up all of the devices 
	# created/verified/removed by a specific user. should the 
	# need arise, do a SQL lookup
	created_dt = models.DateTimeField('Device Initially Added',False,True,editable=False)
	created_tech = models.ForeignKey(User, related_name="+",editable=False)
	verified_dt = models.DateTimeField('Device Last Verified',False,False,null=True,editable=False)
	verified_tech = models.ForeignKey(User, null=True, related_name="+",editable=False)
	removed_dt = models.DateTimeField('Device Removed',False,False,null=True,editable=False)
	removed_tech = models.ForeignKey(User, null=True, related_name="+",editable=False)
	site = models.ForeignKey(Site)
	room = models.CharField(max_length=50)
	make = models.CharField(max_length=50)
	model = models.CharField(max_length=50)
	asset_tag = models.CharField(max_length=50)
	serial_number = models.CharField(max_length=50)
	removed = models.BooleanField(default=False)
	#notes = models.ManyToManyField(Note, null=True) # Superceded by the comments app
	tags = TagField()
	
	def set_tags(self, tags):
		Tag.objects.update_tags(self, tags)
	
	def get_tags(self):
		return Tag.objects.get_for_object(self)
	
	def remove_device(self, tech_id):
		from datetime import datetime
		self.removed_dt = datetime.now()
		self.removed_tech = User.objects.get(id=tech_id)
		self.save()
	
	def verify_device(self, tech_id):
		from datetime import datetime
		self.verified_dt = datetime.now()
		self.verified_tech = User.objects.get(id=tech_id)
		self.save()
	
		
class Ticket(models.Model):
	URGENCY_CHOICES = (
		('0', 'Low'),
		('1', 'Normal'),
		('2', 'High'),
		('3', 'Emergency'),
	)

	created_dt = models.DateTimeField('Ticket Created',False,True,editable=False)
	updated_dt = models.DateTimeField('Ticket Updated',auto_now=True,auto_now_add=True,editable=False)
	due_dt = models.DateTimeField('Due Date',False,False,null=True)
	closed_dt = models.DateTimeField('Ticket Closed',False,False,null=True,editable=False)
	closed_tech = models.ForeignKey(User,null=True,related_name="tickets_closed",editable=False)
	site = models.ForeignKey(Site)
	client = models.ForeignKey(User, related_name="tickets_created")
	description = models.TextField()
	urgency = models.IntegerField(choices=URGENCY_CHOICES)
	techs = models.ManyToManyField(User,null=True, related_name="tickets_serviced")
	#notes = models.ManyToManyField(Note,null=True) # Superceded by the comments app
	tags = TagField()
	
	def set_tags(self, tags):
		Tag.objects.update_tags(self, tags)
	
	def get_tags(self):
		return Tag.objects.get_for_object(self)
	
	def close_ticket(self, tech):
		from datetime import datetime
		self.closed_dt = datetime.now()
		self.closed_tech = tech
		self.save()
	
	def __init__(self, site, client, description, urgency, due_dt=None):
		self.site = site
		self.client = client
		self.description = description
		self.urgency = urgency
		self.techs = site.assigned_techs
		self.due_dt = due_dt
		self.save()
	
	def __unicode__(self):
		return '[%s] Created on %s by %s %s. Desc: %s' % (self.id, self.created_dt, self.client.first_name, self.client.last_name, self.description)
